package com.hexb.smh.controller.management;

import com.hexb.core.model.BaseErrorCodes;
import com.hexb.core.utils.Assert;
import com.hexb.smh.annotations.AopOperationLog;
import com.hexb.smh.entity.Commission;
import com.hexb.smh.entity.EvaluateLevel;
import com.hexb.smh.entity.param.commission.CommissionParam;
import com.hexb.smh.entity.param.evaluate.EvaluateLevelParam;
import com.hexb.smh.entity.view.CommissionTable;
import com.hexb.smh.errors.ErrorCodes;
import com.hexb.smh.service.ICommissionService;
import com.hexb.smh.service.IEvaluateLevelService;
import com.hexb.smh.utils.AccountHelper;
import com.hexb.smh.utils.Converters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import static com.hexb.smh.utils.SmhConstant.ROLE_ADMIN;
import static com.hexb.smh.utils.SmhConstant.ROLE_SUPER_ADMIN;

/**
 * @author : hexb
 */
@RestController
@RequestMapping("/management/evaluate")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "A7.评价等级配置", description = "Server", tags = {"A7.评价等级配置"})
@Slf4j
public class EvaluateLevelController {

    @NonNull
    final private IEvaluateLevelService evaluateLevelService;


    @ApiOperation("添加评级")
    @AopOperationLog(name = "添加评级")
    @RequiresRoles(value = {ROLE_SUPER_ADMIN, ROLE_ADMIN}, logical = Logical.OR)
    @PostMapping
    public int append(@RequestBody @Valid EvaluateLevelParam.EvaluateLevelInsertParam param) {
        return evaluateLevelService.save(param.toEvaluateLevel());
    }


    @ApiOperation("修改评级)")
    @AopOperationLog(name = "修改评级")
    @RequiresRoles(value = {ROLE_SUPER_ADMIN, ROLE_ADMIN}, logical = Logical.OR)
    @PutMapping("/{id}")
    public int update(@RequestBody @Valid EvaluateLevelParam.EvaluateLevelUpdateParam param,
                      @PathVariable("id") Integer id) {
        return evaluateLevelService.update(param.toEvaluateLevel(id));
    }


    @ApiOperation("获取全部评级)")
    @RequiresRoles(value = {ROLE_SUPER_ADMIN, ROLE_ADMIN}, logical = Logical.OR)
    @GetMapping
    public List<EvaluateLevel> list() {
        return evaluateLevelService.selectAll();
    }

    @ApiOperation("删除评级")
    @AopOperationLog(name = "删除评级")
    @RequiresRoles(value = {ROLE_SUPER_ADMIN, ROLE_ADMIN}, logical = Logical.OR)
    @DeleteMapping("/{id}")
    public int delete(@PathVariable("id") @ApiParam(value = "记录id", example = "1", required = true) int id) {
        return evaluateLevelService.delete(id);
    }

    @ApiOperation("删除评级(批量)")
    @AopOperationLog(name = "批量删除评级")
    @RequiresRoles(value = {ROLE_SUPER_ADMIN, ROLE_ADMIN}, logical = Logical.OR)
    @DeleteMapping("/batch/{ids}")
    public int delete(@PathVariable("ids") @ApiParam(value = "记录id列表", example = "1", required = true) Integer[] ids) {
        return evaluateLevelService.deleteBatch(ids);
    }


    @ApiOperation("检查评级名是否重复")
    @RequiresRoles(value = {ROLE_SUPER_ADMIN, ROLE_ADMIN}, logical = Logical.OR)
    @GetMapping("/check/name")
    public Boolean checkName(@ApiParam(value = "评级名", example = "1", required = true) String name, Integer id) {
        Assert.isEmpty(name, BaseErrorCodes.PARAMETER_IS_NULL);
        return evaluateLevelService.checkName(name, id);
    }

}
